#include <QTextCodec>
#include <QXmlStreamWriter>
#include <QDate>
#include <QFile>

#include <SingleTaxReport.h>
#include <defs.h>

#include <iostream>

SingleTaxReport::SingleTaxReport()
{
    documentDetails.code = "F01";
    documentDetails.type = 0;
    documentDetails.subType = "033";
    documentDetails.number = 1;
    documentDetails.version = 2;
    documentDetails.state = 1;

    adminRegion = 10;
    adminDistrict = 29;

    //reportPeriod = 2;

    QDate currentDate;
    //reportMonth = currentDate.currentDate().addMonths(-1).month();
    reportYear = currentDate.currentDate().addMonths(-1).year();

    creationDate = Date(currentDate.currentDate().year(), currentDate.currentDate().month(), currentDate.currentDate().day());

    taxOfficeName = QString::fromUtf8("1029 (НЕДIЮЧА)БРОВАРСЬКА ОБ'ЄДНАНА ДПI");

    hiredWorkersNumber = 0;
}

void SingleTaxReport::SetTaxPayerName(const QString& name)
{
    taxPayerName = name;
}

void SingleTaxReport::SetReportQuarter(short int quarter)
{
    reportPeriod = quarter;
    reportMonth = quarter * 3;
}

void SingleTaxReport::SetReportYear(short int year)
{
    reportYear = year;
}

void SingleTaxReport::SetActivities(const std::vector<Activity> someActivities)
{
    activities = someActivities;
}

void SingleTaxReport::SetPayments(const std::vector<Payment> somePayments)
{
    payments = somePayments;
}

void SingleTaxReport::SetTaxPaid(long int taxPaid)
{
    totalTaxPaid = taxPaid;
}

void SingleTaxReport::SetEarnedSum(long int earnedSum)
{
    totalEarnedSum = earnedSum;
}

bool SingleTaxReport::WriteToFile(const std::string& fileName)
{    
    QFile file(fileName.c_str());
    file.open(QIODevice::WriteOnly);

    QXmlStreamWriter writer(&file);

    writer.setAutoFormatting(true);
    writer.setAutoFormattingIndent(-1);

    writer.setCodec(QTextCodec::codecForName(gTextEncoding.c_str()));

    //std::cout << "codec: " << QTextCodec::codecForName(gTextEncoding.c_str())->canEncode(taxOfficeName) << "\n";

    writer.writeStartDocument();

    writer.writeStartElement("DECLAR");

    writer.writeNamespace("http://www.w3.org/2001/XMLSchema-instance", "xsi");
    writer.writeAttribute("xsi:noNamespaceSchemaLocation", "F0103302.xsd");

    writer.writeStartElement("DECLARHEAD");

    writer.writeTextElement("TIN", /*taxPayerId.c_str()*/ QString::number(taxPayerId));

    writer.writeTextElement("C_DOC", documentDetails.code.c_str());

    writer.writeTextElement("C_DOC_SUB", documentDetails.subType.c_str());

    writer.writeTextElement("C_DOC_VER", QString::number(documentDetails.version));

    writer.writeTextElement("C_DOC_TYPE", QString::number(documentDetails.type));

    writer.writeTextElement("C_DOC_CNT", QString::number(documentDetails.number));

    writer.writeTextElement("C_REG", QString::number(adminRegion));

    writer.writeTextElement("C_RAJ", QString::number(adminDistrict));

    writer.writeTextElement("PERIOD_MONTH", QString::number(reportMonth));

    writer.writeTextElement("PERIOD_TYPE", QString::number(gReportPeriodType));

    writer.writeTextElement("PERIOD_YEAR", QString::number(reportYear));

    writer.writeTextElement("C_DOC_STAN", QString::number(documentDetails.state));

    writer.writeTextElement("D_FILL", creationDate.ToString().c_str());

    writer.writeTextElement("SOFTWARE", gSoftwareName.c_str());

    // DECLARHEAD
    writer.writeEndElement();

    writer.writeStartElement("DECLARBODY");

    writer.writeTextElement("HSTI", taxOfficeName);

    writer.writeTextElement("HTIN", QString::number(taxPayerId));

    writer.writeTextElement("HNAME", taxPayerName);

    writer.writeTextElement("HZKV", QString::number(reportPeriod));

    writer.writeTextElement("HZY", QString::number(reportYear));

    for (std::vector<Activity>::size_type i = 0; i < activities.size(); i++)
    {
        writer.writeStartElement("T1RXXXXG1S");

        writer.writeAttribute("ROWNUM", QString::number(i + 1));

        writer.writeCharacters(activities[i].code);

        // T1RXXXXG1S
        writer.writeEndElement();
    }

    for (std::vector<Activity>::size_type i = 0; i < activities.size(); i++)
    {
        writer.writeStartElement("T1RXXXXG2S");

        writer.writeAttribute("ROWNUM", QString::number(i + 1));

        writer.writeCharacters(activities[i].name);

        // T1RXXXXG1S
        writer.writeEndElement();
    }

    for (std::vector<Activity>::size_type i = 0; i < activities.size(); i++)
    {
        writer.writeTextElement("R02G" + QString::number(i + 1) + "I", "0");
    }

    for (std::vector<Payment>::size_type i = 0; i < payments.size(); i++)
    {
        writer.writeStartElement("T3RXXXXG1S");

        writer.writeAttribute("ROWNUM", QString::number(i + 1));

        writer.writeCharacters(payments[i].type);

        // T1RXXXXG1S
        writer.writeEndElement();
    }

    for (std::vector<Payment>::size_type i = 0; i < payments.size(); i++)
    {
        writer.writeStartElement("T3RXXXXG2D");

        writer.writeAttribute("ROWNUM", QString::number(i + 1));

        writer.writeCharacters(payments[i].paymentDate.ToString().c_str());

        // T3RXXXXG2D
        writer.writeEndElement();
    }

    for (std::vector<Payment>::size_type i = 0; i < payments.size(); i++)
    {
        writer.writeStartElement("T3RXXXXG3S");

        writer.writeAttribute("ROWNUM", QString::number(i + 1));

        writer.writeCharacters(payments[i].id);

        // T3RXXXXG3S
        writer.writeEndElement();
    }

    writer.writeTextElement("R03G1", QString::number(totalTaxPaid) + ".00");


    writer.writeTextElement("R04G1", QString::number(totalEarnedSum) + ".00");

    writer.writeTextElement("HFILL", creationDate.ToString().c_str());

    // DECLARBODY
    writer.writeEndElement();


    // DECLAR
    writer.writeEndElement();

    writer.writeEndDocument();

    file.close();

    return true;
}
